package com.sftz.modules.minapp.resourceRecord.service;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.sftz.assets.enums.AnswerStatusEnum;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * @author   DuKL 各项进度
 * @Date	 2018年12月21日
 */
@IocBean
public class StudyProgressViewService {

	@Inject
	private StudentStudyCourseViewService studentStudyCourseViewService;

	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	/**
	 * @author   DuKL
	 * @Date	 2018年11月15日
	 * (effect:该课程下学生已完成教学活动占比)
	 */
	public double activityAleryScore(Integer courseId, Integer userId) {
		double stuActivityAleryScore = 0;
		//学生已答作业数量
		Sql activity = Sqls.create(sqlManager
				.get("minapp_resourceRecord_teaching_center_personStudy_activity_report_count"));
		activity.setParam("userId", userId);
		activity.setParam("courseId", courseId);
		double aleryCount = DbSqlUtil.fetchInt(dbDao, activity);
		//该课程下作业的数量
		Sql activityCount = Sqls.create(sqlManager
				.get("minapp_resourceRecord_teaching_center_course_allactivity_list_count"));
		activityCount.setParam("courseId", courseId);
		double actCount = DbSqlUtil.fetchInt(dbDao, activityCount);
		if (aleryCount != 0 && actCount != 0) {
			stuActivityAleryScore = aleryCount / actCount;
			BigDecimal bg = new BigDecimal(stuActivityAleryScore).setScale(2, RoundingMode.UP);
			stuActivityAleryScore = bg.doubleValue() * 100;
		}
		return stuActivityAleryScore;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年11月15日
	 * (effect:该课程下学生已完成作业占比)
	 */
	public double homeAleryScore(Integer courseId, Integer userId) {
		double stuHomeAleryScore = 0;
		//学生已答作业数量
		Sql homework = Sqls.create(sqlManager
				.get("minapp_resourceRecord_teaching_center_personStudy_homework_report_count"));
		homework.setParam("userId", userId);
		homework.setParam("courseId", courseId);
		homework.setParam("status", AnswerStatusEnum.start.intKey());
		double aleryCount = DbSqlUtil.fetchInt(dbDao, homework);
		//该课程下作业的数量
		Sql hwCount = Sqls.create(sqlManager.get("minapp_resourceRecord_teaching_center_course_allhomewor_list_count"));
		hwCount.setParam("courseId", courseId);
		double homeCount = DbSqlUtil.fetchInt(dbDao, hwCount);
		if (aleryCount != 0 && homeCount != 0) {
			stuHomeAleryScore = aleryCount / homeCount;
			BigDecimal bg = new BigDecimal(stuHomeAleryScore).setScale(2, RoundingMode.UP);
			stuHomeAleryScore = bg.doubleValue() * 100;
		}
		return stuHomeAleryScore;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年11月15日
	 * (effect:该课程下学生已完成试卷占比)
	 */
	public double paperAleryScore(Integer courseId, Integer userId) {
		double stuPaperAleryScore = 0;
		//学生已答作业数量
		Sql papeRC = Sqls
				.create(sqlManager.get("minapp_resourceRecord_teaching_center_personStudy_paper_report_count"));
		papeRC.setParam("userId", userId);
		papeRC.setParam("courseId", courseId);
		papeRC.setParam("status", AnswerStatusEnum.start.intKey());
		double aleryCount = DbSqlUtil.fetchInt(dbDao, papeRC);
		//该课程下作业的数量
		Sql paperCount = Sqls
				.create(sqlManager.get("minapp_resourceRecord_teaching_center_course_allpaper_list_count"));
		paperCount.setParam("courseId", courseId);
		double paCount = DbSqlUtil.fetchInt(dbDao, paperCount);
		if (aleryCount != 0 && paCount != 0) {
			stuPaperAleryScore = aleryCount / paCount;
			BigDecimal bg = new BigDecimal(stuPaperAleryScore).setScale(2, RoundingMode.UP);
			stuPaperAleryScore = bg.doubleValue() * 100;
		}
		return stuPaperAleryScore;
	}
}